package cn.com.thingo.shyintelligent.ui.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.th.opensdk.openapi.SceneGroupInfo;
import com.th.opensdk.openapi.SceneInfo;
import com.th.opensdk.openapi.THOpenSDK;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.com.thingo.shyintelligent.R;
import cn.com.thingo.shyintelligent.app.App;
import cn.com.thingo.shyintelligent.base.BaseActivity;
import cn.com.thingo.shyintelligent.base.contract.WindowContract;
import cn.com.thingo.shyintelligent.presenter.WindowPresenter;
import cn.com.thingo.shyintelligent.ui.adapter.WindowListAdapter;
import cn.com.thingo.shyintelligent.ui.adapter.decoration.RecycleViewDivider;
import cn.com.thingo.shyintelligent.util.ToastUtil;

public class WindowActivity extends BaseActivity<WindowPresenter> implements WindowContract.View, WindowListAdapter.CallBack {
    @BindView(R.id.toolbar_left)
    ImageView ivLeft;
    @BindView(R.id.toolbar_title)
    TextView tvTitle;
    @BindView(R.id.window_list_rv)
    RecyclerView rvList;
    private THOpenSDK thOpenSDK;
    private int currentRoomId;
    private List<SceneInfo> sceneList = new ArrayList<>();
    private WindowListAdapter adapter;

    @Override
    protected int getLayout() {
        return R.layout.activity_window;
    }

    @Override
    protected void initEventAndData() {
        thOpenSDK = THOpenSDK.getInstance();
        currentRoomId = getIntent().getIntExtra("current_room_id", -1);
        initView();
        initData();
    }

    private void initData() {
        List<SceneInfo> sceneInfoList = new ArrayList<>();
        List<SceneGroupInfo> sceneGroupList = thOpenSDK.getSceneGroupList();//场景列表 窗户暂时依托场景
        for (int i = 0; i < sceneGroupList.size(); i++) {
            List<SceneInfo> sceneList = sceneGroupList.get(i).getSceneList();
            if (sceneList.size() > 0) {
                sceneInfoList.addAll(sceneList);
            }
        }
        if (sceneInfoList.size() > 0) {
            for (int i = 0; i < sceneInfoList.size(); i++) {
                int roomId = sceneInfoList.get(i).getRoomId();
                if (roomId == currentRoomId && sceneInfoList.get(i).getSceneName().startsWith("窗户-")) {
                    sceneList.add(sceneInfoList.get(i));
                }
            }
        }
        adapter.notifyDataSetChanged();
    }

    private void initView() {
        ivLeft.setImageResource(R.mipmap.back);
        tvTitle.setText(R.string.window);
        rvList.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new WindowListAdapter(sceneList, mContext);
        rvList.addItemDecoration(new RecycleViewDivider(mContext, LinearLayoutManager.VERTICAL, 2, getResources().getColor(R.color.gray_12)));
        adapter.setCallBack(this);
        rvList.setAdapter(adapter);
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    public void callBack(final SceneInfo sceneInfo) {
        if (App.getInstance().isScenesStatus()) {
            //场景开关控制
            App.getInstance().setScenesStatus(false);
            long controlId = sceneInfo.getControlId();
            App.getInstance().setCurrentScenesId(controlId);
            new Thread() {
                @Override
                public void run() {
                    super.run();
                    sceneInfo.run();
                }
            }.start();
        } else {
            ToastUtil.shortShow("有其他窗户的操作正在部署中...");
        }
    }


    @OnClick({R.id.toolbar_left})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.toolbar_left:
                finish();
                break;
        }
    }
}
